package com.h2cloud.config;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author: wulongkai
 * @create: 2022-09-20 19:39
 **/
public class StringTypeHander extends BaseTypeHandler<String> {
    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, String s, JdbcType jdbcType) throws SQLException {
        preparedStatement.setString(i, "," + s + ",");
    }

    @Override
    public String getNullableResult(ResultSet resultSet, String s) throws SQLException {
        String dbValue = resultSet.getString(s);
        String convertValue = dbValue.substring(1).substring(0, dbValue.length() - 2);
        System.out.println("convertValue1： " + convertValue);
        return convertValue;
    }

    @Override
    public String getNullableResult(ResultSet resultSet, int i) throws SQLException {
        String dbValue = resultSet.getString(i);
        String convertValue = dbValue.substring(1).substring(0, dbValue.length() - 2);
        System.out.println("convertValue2： " + convertValue);
        return convertValue;
    }

    @Override
    public String getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        String dbValue = callableStatement.getString(i);
        String convertValue = dbValue.substring(1).substring(0, dbValue.length() - 2);
        System.out.println("convertValue3： " + convertValue);
        return convertValue;
    }
}
